package com.carleasoft.mps.adu.business.role.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carleasoft.mps.adu.business.role.bean.po.AuthorityRoleInfoPagePo;
import com.carleasoft.mps.adu.business.role.bean.po.AuthorityRoleInfoPo;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.user.GrantUserVo;
import com.carleasoft.mps.adu.business.role.bean.po.RoleParamsPo;
import com.carleasoft.mps.adu.business.role.bean.vo.AuthorityDataPermsVo;
import com.carleasoft.mps.adu.business.role.bean.vo.AuthorityRoleInfoPageVo;
import com.carleasoft.mps.adu.business.role.bean.vo.AuthorityRoleVo;
import com.carleasoft.mps.adu.business.role.service.AuthorityRoleService;
import com.carleasoft.mps.adu.business.role.service.GrantPermsService;
import com.carleasoft.mps.core.constant.ErrorInfoEnum;
import com.carleasoft.mps.core.result.ResultBody;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.PageResult;
import com.carleasoft.mps.data.mybatisplus.page.Pagehelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>Description: Role controller</p>
 * @since 2020-05-11
 * @author ztm
 */
@RestController
@RequestMapping("/perms")
public class AuthorityRoleController {

    @Autowired
    AuthorityRoleService authorityRoleService;
    @Autowired
    GrantPermsService grantPermsService;

    /**
     * 添加权限
     * ok
     * @param
     * @return ResultBody
     */
    @PostMapping("/addOrUpdataRole")
    public ResultBody addOrUpdataRole(@RequestBody AuthorityRoleInfoPo authorityRoleInfoPo){
        authorityRoleService.addOrUpdataRole(authorityRoleInfoPo);
        return new ResultBody(ErrorInfoEnum.SUCCESS);
    }

    /**
     * 删除权限
     * ok
     * @param
     * @return ResultBody
     */
    @PostMapping("/permsDelete")
    public ResultBody deleteRole(@RequestBody RoleParamsPo roleParamsPo){
        //获取当前登陆人信息
        authorityRoleService.deleteRole(roleParamsPo);
        return new ResultBody(ErrorInfoEnum.SUCCESS);
    }

    /**
     * 获取信息列表,分页
     * ok
     * @param authorityRoleInfoPagePo
     * @return ResultBody
     */
    @PostMapping("/permsPageQuery")
    public R<Page<AuthorityRoleInfoPageVo>> pageQuery(@RequestBody AuthorityRoleInfoPagePo authorityRoleInfoPagePo){
        //获取当前登陆人信息
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        authorityRoleInfoPagePo.setOrganizationCode(user.getOrgCodeJoin());
        Page<AuthorityRoleInfoPagePo> page = new Pagehelper(authorityRoleInfoPagePo);
        Page<AuthorityRoleInfoPageVo> results = authorityRoleService.pageQuery(page,authorityRoleInfoPagePo);
//        PageResult<AuthorityRoleInfoPageVo> pageResult = new PageResult<AuthorityRoleInfoPageVo>(page.getTotal(),results);
        return R.ok(results);
    }


    /**
     * 权限配置时查询目标对象所有能被赋予的所有权限
     * ok
     * @param
     * @return ResultBody
     */
    @PostMapping("/permsGrant")
    public ResultBody getAllRolesByLoginUserCode(@RequestBody RoleParamsPo roleParamsPo){

        Map<String,AuthorityRoleVo> authorityRoleInfoPageVoMap= grantPermsService.getTargetAndLoginUserRoles(roleParamsPo);
        return new ResultBody<Map<String,AuthorityRoleVo>>(authorityRoleInfoPageVoMap);
    }

    /**
     * 查询目标对象所拥有的所有权限
     * ok
     * @param
     * @return ResultBody
     */
    @PostMapping("/getAllRolesByTargetCode")
    public ResultBody getAllRolesByTargetCode(@RequestBody RoleParamsPo roleParamsPo){

        AuthorityRoleVo authorityRoleVo= grantPermsService.getAllRolesByTargetCode(roleParamsPo);
        return new ResultBody<AuthorityRoleVo>(authorityRoleVo);
    }

    /**
     * 查询登录用户所拥有的所有权限详情，并将所属组织和所属部分进行解析
     * ok
     * @param
     * @return ResultBody
     */
    @PostMapping("/getAllPermsByUser")
    public ResultBody getAlPermsByTargetCode(){

        AuthorityRoleVo authorityRoleVo= grantPermsService.getAllPermsByUser(true,true);

        return new ResultBody<AuthorityDataPermsVo>(authorityRoleVo.getDataPermsVo());
    }

    /**
     * 查询登录用户所拥有的所有权限
     * ok
     * @param
     * @return ResultBody
     */
    @PostMapping("/getAllPermsInc")
    public ResultBody getAlPermsIncByTargetCode(){

        AuthorityRoleVo authorityRoleVo= grantPermsService.getAllPermsByUser(true,false);

        return new ResultBody<AuthorityDataPermsVo>(authorityRoleVo.getDataPermsVo());
    }

    /**
     * 反向授权接口
     * @param
     * @return void
     */
    @PostMapping("/revGrantRole")
    public R revGrantRole(@RequestBody GrantUserVo grantUserVo) {
        authorityRoleService.revGrantRole(grantUserVo.getUserCode(),grantUserVo.getUserName(),grantUserVo.getType());
        return R.ok();
     }

    /**
     * 反向授权接口
     * @param
     * @return void
     */
    @PostMapping("/delGrantRole")
    public R delGrantRole(@RequestBody GrantUserVo grantUserVo) {
        authorityRoleService.delGrantRole(grantUserVo.getUserCode(),grantUserVo.getUserName(),grantUserVo.getType());
        return R.ok();
    }

}
